#include "Spatial.h"
#include "Renderer.h"

namespace graphics
{

Spatial::Spatial()
{
	World = identity44();
	Local = identity44();
	m_Parent = NULL;
}
void Spatial::UpdateGS(float dt)
{
	UpdateWorldData(dt);
}
void Spatial::UpdateWorldData(float dt)
{
		//If it's not the root (If it has a parent)
		if( m_Parent != NULL )
		{
			World = m_Parent->World * Local;
		}
		else //If it's the root
			World = Local;
}
Spatial* Spatial::getParent()
{
	return m_Parent;
}
void Spatial::setParent(Spatial* SpatialPtr)
{
	if(SpatialPtr == NULL) m_Parent = NULL;
	else m_Parent = SpatialPtr;
}
void Spatial::onDraw(Renderer& RendererAddress)
{
	Draw(RendererAddress);
}

/*===================================================================================*/
/*RENDER STATES*/
/*===================================================================================*/
void Spatial::SetRenderState(RS state)
{
	bool Set = true;
	std::vector<RS>::iterator Iter;
	for(Iter = RenderStates.begin(); Iter != RenderStates.end(); Iter++)
	{
		if((*Iter).RS_Type == state.RS_Type) Set = false; //If it is already in states vector don't add it
	}
	if(Set) RenderStates.push_back(state);
}
void Spatial::SetRenderState(std::vector<RS> states)
{
	std::vector<RS>::iterator Iter;
	for(Iter = states.begin(); Iter != states.end(); Iter++)
	{
		SetRenderState(*Iter);
	}
}
void Spatial::RemoveRenderState(RS state)
{
	std::vector<RS>::iterator Iter;
	for(Iter = RenderStates.begin();Iter != RenderStates.end();)
	{
		if((*Iter).RS_Type == state.RS_Type) 
		{
			Iter = RenderStates.erase(Iter);
		}
		else Iter++;
	}
}

std::vector<RS> Spatial::GetRenderStates()
{
	return RenderStates;
}

RS_AlphaBlend Spatial::GetRenderState_AlphaBlend()
{
	return RenderStates_AlphaBlend;
}
RS_Material Spatial::GetRenderState_Material()
{
	return RenderStates_Material;
}

}/* namespace graphics */